﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class Modules_Personal_TargetAdd : System.Web.UI.Page
{
    IPerf_Personal_TargetHandler targetHandler = new Perf_Personal_TargetManage();
    IPerf_Personal_TargetTypeHandler ttHandler = new Perf_Personal_TargetTypeManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("301011") || strPur.Contains("301012"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    private void dataBind()
    {
        hdnTargetOwnerID.Value = Request["oid"];
        FillStatus();
        FillTargetType();
        FillIsReuqired();

        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            int ID = 0;
            int.TryParse(Request["ID"], out ID);

            if (ID != 0)
            {
                Perf_Personal_TargetQuery query = targetHandler.GetPerf_Personal_TargetQuery(ID);
                if (query != null)
                {
                    txtCode.Text = query.Code;
                    ddlTargetType.SelectedValue = query.TargetType_ID.ToString();
                    txtScore.Text = query.Score.ToString();
                    txtName.Text = query.Name;
                    txtRemark.Text = query.Remark;
                    ddlStatus.SelectedValue = query.Status.ToString();
                    ddlIsRequired.SelectedValue = query.IsRequired.ToString();
                }
            }
        }
    }

    private void FillStatus()
    {
        DataTable dtStatus = new DataTable();
        dtStatus.Columns.Add("Value");
        dtStatus.Columns.Add("Name");
        DataRow drStatus;

        drStatus = dtStatus.NewRow();
        drStatus[0] = "1";
        drStatus[1] = Enums.GetNormalStatusName(Enums.NormalStatus.Vallid);
        dtStatus.Rows.Add(drStatus);

        drStatus = dtStatus.NewRow();
        drStatus[0] = "0";
        drStatus[1] = Enums.GetNormalStatusName(Enums.NormalStatus.Invalid);
        dtStatus.Rows.Add(drStatus);

        ddlStatus.DataSource = dtStatus;
        ddlStatus.DataTextField = "Name";
        ddlStatus.DataValueField = "Value";
        ddlStatus.DataBind();
    }

    private void FillTargetType()
    {
        DataTable dtTargetType = ttHandler.GetPerf_Personal_TargetTypeList(" Status = @Status", new SqlParameter[] { new SqlParameter("@Status", (int)Enums.NormalStatus.Vallid) }, " id asc");
        ddlTargetType.DataTextField = "Name";
        ddlTargetType.DataValueField = "ID";
        ddlTargetType.DataSource = dtTargetType;
        ddlTargetType.DataBind();
    }

    private void FillIsReuqired()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Value");
        dt.Columns.Add("Name");
        DataRow row;

        row = dt.NewRow();
        row[0] = 0;
        row[1] = Enums.GetYesNoStatus(Enums.YesNoStatus.Yes);
        dt.Rows.Add(row);

        row = dt.NewRow();
        row[0] = 1;
        row[1] = Enums.GetYesNoStatus(Enums.YesNoStatus.No);
        dt.Rows.Add(row);

        ddlIsRequired.DataSource = dt;
        ddlIsRequired.DataTextField = "Name";
        ddlIsRequired.DataValueField = "Value";
        ddlIsRequired.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if ("".Equals(txtCode.Text.Trim()))
        {
            Jscript.Alert("代码不能为空", this.Page);
            return;
        }
        if ("".Equals(txtName.Text.Trim()))
        {
            Jscript.Alert("名称不能为空", this.Page);
            return;
        }
        if ("".Equals(txtScore.Text.Trim()))
        {
            Jscript.Alert("分值不能为空", this.Page);
            return;
        }
        //if (Request["ID"] != null && Request["ID"] != string.Empty)
        //{
        //    if (isCodeExists(txtCode.Text.Trim(), int.Parse(Request["ID"])))
        //    {
        //        Jscript.Alert("代码已存在", this.Page);
        //        return;
        //    }
        //}
        //else
        //{
        //    if (isCodeExists(txtCode.Text.Trim()))
        //    {
        //        Jscript.Alert("代码已存在", this.Page);
        //        return;
        //    }
        //}

        Perf_Personal_TargetQuery query = new Perf_Personal_TargetQuery();
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            query = targetHandler.GetPerf_Personal_TargetQuery(int.Parse(Request["ID"]));
        }
        query.TargetOwner_ID = int.Parse(hdnTargetOwnerID.Value);
        query.Code = txtCode.Text.Trim();
        query.Name = txtName.Text.Trim();
        query.TargetType_ID = int.Parse(ddlTargetType.SelectedValue);
        query.Score = float.Parse(txtScore.Text.Trim());
        query.Status = int.Parse(ddlStatus.SelectedValue.ToString());
        query.IsRequired = int.Parse(ddlIsRequired.SelectedValue.ToString());
        query.User_ID = int.Parse(Session["CurrentUserID"].ToString());
        query.Remark = txtRemark.Text;
        query.Create_Date = DateTime.Now;

        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            if (targetHandler.UpdatePerf_Personal_Target(query))
            {
                Jscript.Alert("更新成功！", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
        }
        else
        {
            if (targetHandler.InsertPerf_Personal_Target(query))
            {
                Jscript.Alert("添加成功！", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
        }
    }


    private bool isCodeExists(string code)
    {
        bool istrue = false;

        DataTable dt = targetHandler.GetPerf_Personal_TargetList(" Code = @Code", new SqlParameter[] { new SqlParameter("@Code", code) });
        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

    private bool isCodeExists(string code, int postId)
    {
        bool istrue = false;

        DataTable dt = targetHandler.GetPerf_Personal_TargetList("Code=@Code and ID!=@Id", new SqlParameter[] { new SqlParameter("@Code", code), new SqlParameter("@Id", postId.ToString()) });

        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

}
